package edu.ccut.saturn.workflow.delegate;

import java.sql.Connection;
import java.util.ArrayList;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;

public class SetDelegate extends DefaultComponent{
	public void execute(IParameterContext parameterContext) throws Exception {
		String delegate_reason = (String)parameterContext.getParameterValue(4);
		String delegate_from = (String)parameterContext.getParameterValue(3);
		String delegate_to_user_type=(String)parameterContext.getParameterValue(2);
		String delegate_to = (String)parameterContext.getParameterValue(1);
		String workitemId = (String)parameterContext.getParameterValue(0);	
		String operate_type = (String)parameterContext.getParameterValue(5);//2:代办 3:协办
		Connection  conn = parameterContext.getConnection();
		//查询工作项实例
		SaturnData workitemInst = Operation.dataBase()
				.queryByPrimaryKey(Constants.SATURNDATA_WF_WORKITEM_INSTANCE, workitemId, conn);
		//更新工作项的业务状态
		workitemInst.set("bizstate", operate_type);
		Operation.dataBase().updateByPrimaryKey(workitemInst, "", conn);
		//处理代办表
		String systime = Operation.baseService().GetSystemTime();//当前系统时间
		SaturnData delegateInst = WorkFlowTools.getDelegateInstance();
		//为代办数据项赋值
		delegateInst.set("process_instance_id", workitemInst.get("process_instance_id")+"");
		delegateInst.set("activity_define_id", workitemInst.get("activity_define_id")+"");
		delegateInst.set("workitem_id", workitemId);
		delegateInst.set("delegate_type", operate_type);
		delegateInst.set("delegate_reason", delegate_reason);
		delegateInst.set("delegate_to", delegate_to);
		delegateInst.set("origin", delegate_to_user_type);
		delegateInst.set("delegate_from", delegate_from);
		delegateInst.set("start_time", systime);
		delegateInst.set("state", Constants.DELEGATE_STATE_RUNNING);
		//插入代办表
		Operation.dataBase().insert(delegateInst, conn);
		
		SaturnData delegateMesgInst = WorkFlowTools.getDelegateMesgInstance();
		//为代办数据项赋值
		delegateMesgInst.set("content", delegate_reason);
		delegateMesgInst.set("message_from",delegate_from);
		delegateMesgInst.set("workitem_id", workitemId);
		delegateMesgInst.set("operate_type", operate_type);//1（协办）,2（代办）
		delegateMesgInst.set("process_instance_id", workitemInst.get("process_instance_id")+"");
		delegateMesgInst.set("time", systime);
		//插入代办表
		Operation.dataBase().insert(delegateMesgInst, conn);
		
		//设此工作项的其他参与者的partiintype='N'
		String sql = "update wf_wiparticipant set partiintype='N' where workitemid="+workitemId;
		Operation.dataBase().updateBySQL(sql, conn);
		
		//处理参与者表
		ArrayList<SaturnData> userDataList = WorkFlowTools.getUserDataListByTypeAndId(delegate_to_user_type, delegate_to, conn);		
		if(userDataList!=null){
			for(SaturnData user: userDataList){					
				SaturnData participantInst = WorkFlowTools.getParticipantInstanceWithValues(user.get("id")+"", user.get("username")+"");
				participantInst.set("workitemid",workitemId);
				participantInst.set("workitemstate",operate_type);
				participantInst.set("partiintype","Y");//有执行权限
				//插入参与者表
				Operation.dataBase().insert(participantInst, conn);
			}
		}else{
			throw new Exception("user not found");
		}
	}
}
